package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.CreditEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.TextStringUtils;
import com.xilihui.xlh.core.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/6.
 */

public class CreditScoreActivity extends BaseCompatActivity implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    TextView tv_level;
    ImageView iv_level;
    TextView tv_score;
    TextView tv_time;
    TextView tv_xy_a;
    TextView tv_xy_b;
    TextView tv_xy_c;
    TextView tv_a_hint;
    TextView tv_b_hint;
    TextView tv_c_hint;
    TextView tv_a;
    TextView tv_b;
    TextView tv_c;
    TextView tv_guizhe;
    
    BaseAdapter<CreditEntity.CreditListBean> baseAdapter;
    
    ArrayList<CreditEntity.CreditListBean> data = new ArrayList<>();
    
    int page = 1;
    
    @Override
    public int getLayout() {
        return R.layout.activity_credit_score;
    }

    @Override
    public String initTitle() {
        return "我的信用分";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        
        initHead();
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingMoreEnabled(false);
        baseAdapter = new BaseAdapter<CreditEntity.CreditListBean>(this,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_integral;
            }

            @Override
            protected void getView(BaseViewHolder holder, CreditEntity.CreditListBean entity, int position) {
                
                int score = Integer.valueOf(entity.getScore());
                
                if(score>=0){
                    holder.setTextColor(CreditScoreActivity.this,R.id.tv_num,R.color.tv_fb4e44);
                }else {
                    holder.setTextColor(CreditScoreActivity.this,R.id.tv_num,R.color.tv_333333);
                }
                
                holder.setText(R.id.tv_title,entity.getType());
                holder.setText(R.id.tv_time,entity.getAdd_time());
                holder.setText(R.id.tv_num,entity.getScore());
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        
    }
    
    public void initHead(){
        View view = LayoutInflater.from(this).inflate(R.layout.recycler_item_credit_score_head,null);
        tv_level = (TextView)view.findViewById(R.id.tv_level);
        iv_level = (ImageView)view.findViewById(R.id.iv_level); 
        tv_score = (TextView)view.findViewById(R.id.tv_score);
        tv_time = (TextView)view.findViewById(R.id.tv_time);
        tv_xy_a = (TextView)view.findViewById(R.id.tv_xy_a);
        tv_xy_b = (TextView)view.findViewById(R.id.tv_xy_b);
        tv_xy_c = (TextView)view.findViewById(R.id.tv_xy_c);
        tv_a_hint = (TextView)view.findViewById(R.id.tv_a_hint);
        tv_b_hint = (TextView)view.findViewById(R.id.tv_b_hint);
        tv_c_hint = (TextView)view.findViewById(R.id.tv_c_hint);
//        tv_a = (TextView)view.findViewById(R.id.tv_a);
//        tv_b = (TextView)view.findViewById(R.id.tv_b);
//        tv_c = (TextView)view.findViewById(R.id.tv_c);
        tv_guizhe = (TextView)view.findViewById(R.id.tv_guizhe);
        tv_guizhe.setOnClickListener(this);

        TextStringUtils.setSpanTextView(tv_xy_a,"信用A级",new TextStringUtils.SpanColorHolder(
                2,3,getResources().getColor(R.color.tv_a)
        ));
        
        TextStringUtils.setSpanTextView(tv_xy_b,"信用B级",new TextStringUtils.SpanColorHolder(
                2,3,getResources().getColor(R.color.tv_b)
        ));
        
        TextStringUtils.setSpanTextView(tv_xy_c,"信用C级",new TextStringUtils.SpanColorHolder(
                2,3,getResources().getColor(R.color.tv_c)
        ));
        
        
        recyclerView.addHeaderView(view);
    }

    @Override
    public void initData() {
        getData(true);
    }
    
    
    public void getData(boolean isLoading){
        MineRequest.credit(this,page)
                .compose(DoTransform.<CreditEntity>applyScheduler(this,isLoading))
                .compose(CreditScoreActivity.this.<CreditEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<CreditEntity>(this) {
                    @Override
                    protected void onSuccess(CreditEntity entity) {
                        
                        if(page==1){
                            data.clear();
                            String level = entity.getCredit_level();
                            setImage(level);
                            tv_level.setText("信用"+level+"级");
                            tv_score.setText(entity.getCredit_score());
                            tv_time.setText("评估时间："+entity.getBegin_time());
                            String A = entity.getA();
                            String B = entity.getB();
                            String C = entity.getC();
//                            tv_a.setText(A);
//                            tv_b.setText(B);
//                            tv_c.setText(C);
                          
                            tv_a_hint.setText("信用分≥"+entity.getCredit_a());
                            tv_b_hint.setText(entity.getCredit_b()+"<信用分<"+entity.getCredit_a());
                            tv_c_hint.setText("信用分≤"+entity.getCredit_b());
                            
                        }
                        
                        data.addAll(entity.getCreditList());
                        baseAdapter.setList(data);
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
    }

    
    public void setImage(String levet){
        
        if(levet.equals("A")){
            iv_level.setImageResource(R.mipmap.level_a);
            tv_score.setTextColor(getResources().getColor(R.color.tv_a));
        }else if(levet.equals("B")){
            iv_level.setImageResource(R.mipmap.level_b);
            tv_score.setTextColor(getResources().getColor(R.color.tv_b));
        }else if(levet.equals("C")){
            iv_level.setImageResource(R.mipmap.level_c);
            tv_score.setTextColor(getResources().getColor(R.color.tv_c));
        }
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page +1;
        getData(false);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(CreditScoreActivity.this,WebActivity.class);
        intent.putExtra("type",3);
        startActivity(intent);
    }
}
